//
//  NSString+RegexCategory.m
//  GSCalendar
//
//  Created by guoshikeji_a on 2018/9/5.
//  Copyright © 2018年 tanyin. All rights reserved.
//

#import "NSString+RegexCategory.h"

//标点符号 * . ? + $ ^ [ ] ( ) { } | \ /
#define RegexBdfh @"~`!@\%&-_=;:\"',<>\\$\\{\\}\\|"//@"\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5"

@implementation NSString (RegexCategory)

#pragma mark - 正则相关
+ (BOOL)isValidateByRegex:(NSString *)regex withString:(NSString *)string{
    if (!string) {
        return false;
    }
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pre evaluateWithObject:string];
}
//判断是否是有效手机号码 全数字 长度11
+ (BOOL)isPhoneNumber:(NSString *)phoneNumber {
    NSString *allNumberRegex = @"^\\d+$";//@"^[0-9]+$";
    NSString *elevenRegex = @"^.{11}$";
    //
    BOOL number = [self isValidateByRegex:allNumberRegex withString:phoneNumber];
    BOOL eleven = [self isValidateByRegex:elevenRegex withString:phoneNumber];
    return (number&&eleven);
}

//邮箱
+ (BOOL)isEmailAddress:(NSString *)emailAddresss {
    NSString *emailRegex = @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    return [self isValidateByRegex:emailRegex withString:emailAddresss];
}

+ (GSCCommonLoginAccountType)gscLoginAccountTypeWithString:(NSString *)string {
    BOOL isPhoneNumber = [self isPhoneNumber:string];
    if (isPhoneNumber) {
        return GSCCommonLoginAccountTypePhone;
    }
    BOOL isEmailAddress = [self isEmailAddress:string];
    if (isEmailAddress) {
        return GSCCommonLoginAccountTypeEmail;
    }
    return GSCCommonLoginAccountTypeUnknown;
}

//密码  6-20个英文字母、数字或字符（除空格），且字母、 数字和标点符号至少包含两种
+ (BOOL)fda {
    NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:@"tete"];
    return isMatch;
}

+ (BOOL)gsLoginPasswordRegex:(NSString *)password {
    //长度
    if (password.length<6 || password.length>20) {
        return false;
    }
    //是否只包含数字 字母 和标点
    NSString *pureRegexString = [NSString stringWithFormat:@"^[0-9a-zA-Z%@]+$",RegexBdfh];
    BOOL pureRegex = [self isValidateByRegex:pureRegexString withString:password];
    if (!pureRegex) {
        return false;
    }
    //纯数字
    NSString *allNumberRegexString = @"^\\d+$";
    BOOL allNumberRegex = [self isValidateByRegex:allNumberRegexString withString:password];
    if (allNumberRegex) {
        return false;
    }
    //纯字母
    NSString *allLetterRegexString = @"^[a-zA-Z]+$";
    BOOL allLetterRegex = [self isValidateByRegex:allLetterRegexString withString:password];
    if (allLetterRegex) {
        return false;
    }
    //纯标点符号
    NSString *allBdRegexString = [NSString stringWithFormat:@"^[%@]+$",RegexBdfh];
    BOOL allBdRegex = [self isValidateByRegex:allBdRegexString withString:password];
    if (allBdRegex) {
        return false;
    }
    //
    return true;
}


@end
